package com.qingcheng.controller.system;

import com.alibaba.dubbo.config.annotation.Reference;
import com.qingcheng.entity.PageResult;
import com.qingcheng.entity.Result;
import com.qingcheng.pojo.system.Admin;
import com.qingcheng.pojo.system.AdminRoleCompose;
import com.qingcheng.service.system.AdminService;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @Reference
    private AdminService adminService;

    @GetMapping("/findAll")
    public List<Admin> findAll(){
        return adminService.findAll();
    }

    @GetMapping("/findPage")
    public PageResult<Admin> findPage(int page, int size){
        return adminService.findPage(page, size);
    }

    @PostMapping("/findList")
    public List<Admin> findList(@RequestBody Map<String,Object> searchMap){
        return adminService.findList(searchMap);
    }

    @PostMapping("/findPage")
    public PageResult<Admin> findPage(@RequestBody Map<String,Object> searchMap,int page, int size){
        return  adminService.findPage(searchMap,page,size);
    }

    @GetMapping("/findById")
    public AdminRoleCompose findById(Integer id){
        AdminRoleCompose adminRoleCompose = adminService.findById(id);
        return adminRoleCompose;
    }




    @PostMapping("/add")
    public Result add(@RequestBody AdminRoleCompose adminRoleCompose){
        Admin admin = adminRoleCompose.getAdmin();
        String password = admin.getPassword();
        if (password!=null){
            String Pwd = BCrypt.hashpw(password, BCrypt.gensalt());
            admin.setPassword(Pwd);
        }
        adminRoleCompose.setAdmin(admin);
        adminService.add(adminRoleCompose);
        return new Result();
    }



    @PostMapping("/update")
    public Result update(@RequestBody AdminRoleCompose adminRoleCompose){
        Admin admin = adminRoleCompose.getAdmin();
        String password = admin.getPassword();
        if (password==null){
            adminService.update(adminRoleCompose);
        }else {
            String Pwd = BCrypt.hashpw(password, BCrypt.gensalt());
            admin.setPassword(Pwd);
            adminRoleCompose.setAdmin(admin);
            adminService.update(adminRoleCompose);
        }

        return new Result();
    }



    @GetMapping("/delete")
    public Result delete(Integer id){
        adminService.delete(id);
        return new Result();
    }



    @PostMapping("/updatePwd")
    public Result updatePwd(@RequestBody Map<String,Object> paraMap){
        //获取根据用户获取数据库原密码
        Map map = new HashMap();
        map.put("loginName",paraMap.get("username"));
        map.put("satstus","1");
        List<Admin> adminList = adminService.findList(map);
        if (adminList.size()==0){
            throw new RuntimeException("没有此用户");
        }
        String oldPwd = BCrypt.hashpw(String.valueOf(paraMap.get("oldPwd")), BCrypt.gensalt());
        map.put("password",BCrypt.hashpw(String.valueOf(paraMap.get("newPwd")),BCrypt.gensalt()));

        for (Admin admin : adminList) {
            if (!oldPwd.equals(admin.getPassword())){
                return new Result();
            }
            admin.setPassword(String.valueOf(map.get("password")));
            adminService.update(admin);
        }
        return new Result();
    }




}
